# 按照中位数填充（median） 列中为0 的数
import numpy as np
import pandas as pd
import os

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)

pd.set_option('display.max_columns', None); pd.set_option('display.max_rows', None);  # to display all columns and rows
pd.set_option('display.float_format', lambda x: '%.2f' % x) # The number of numbers that will be shown after the comma.

df = pd.read_csv("../DATA/diabetes.csv")

print(df.shape)

print(df.describe([0.10,0.25,0.50,0.75,0.90,0.95,0.99]).T)

print(df["Outcome"].value_counts()*100/len(df))

# 查看 数据为空情况，都为非空,但是有大量数据为0。
print(df.isnull().sum())

# 将0替换成NaN
df[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']] = df[['Glucose','BloodPressure','SkinThickness','Insulin','BMI']].replace(0, np.NaN)

print(df.isnull().sum())

def carp(x,y):
    z = x*y
    return z
# 查看某一列的，按照outcome分组的中位数
def median_target(var):
    temp = df[df[var].notnull()]
    temp = temp[[var, 'Outcome']].groupby(['Outcome'])[[var]].median().reset_index()
    return temp

columns = df.columns
columns = columns.drop("Outcome")

print(columns)

print(median_target('Glucose'))
# 先根据Outcome进行分组，然后进行中位数填充
for col in columns:
    df.loc[(df['Outcome'] == 0 ) & (df[col].isnull()), col] = median_target(col)[col][0]
    df.loc[(df['Outcome'] == 1 ) & (df[col].isnull()), col] = median_target(col)[col][1]

df.loc[(df['Outcome'] == 0 ) & (df["Pregnancies"].isnull()), "Pregnancies"]
df[(df['Outcome'] == 0 ) & (df["BloodPressure"].isnull())]

print(df.isnull().sum())

df.to_csv('./fill_Nan_median.csv')